
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class UserAdd extends JFrame{
    public UserAdd(String userid){

        SQLOperation sql=new SQLOperation();

        setTitle("User Add");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Please complete the following information.");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);

        JPanel info=new JPanel(new GridLayout(5,1,5,5));

        JPanel userPanel=new JPanel();

        userPanel.add(new JLabel("   User Name:"));
        JTextField userName=new JTextField(18);
        userPanel.add(userName);
        info.add(userPanel);

        JPanel phonePanel=new JPanel();

        phonePanel.add(new JLabel("   phoneNum:"));
        JTextField phoneNum=new JTextField(18);
        phonePanel.add(phoneNum);
        info.add(phonePanel);

        JPanel genderPanel=new JPanel();

        genderPanel.add(new JLabel("        gender:"));
        JTextField gender=new JTextField(18);
        genderPanel.add(gender);
        info.add(genderPanel);

        JPanel dobPanel=new JPanel();

        dobPanel.add(new JLabel("DateOfBirth:"));
        JTextField dob=new JTextField(18);
        dobPanel.add(dob);
        info.add(dobPanel);

        JPanel familyPanel=new JPanel();

        familyPanel.add(new JLabel("FamilyID:"));
        JComboBox family=new JComboBox();
        family.addItem("----------");
        String[] familyAttribute={"familyID"};
        Object[][] familyList=sql.search("Family",familyAttribute,"shanchu",1);
        for(int i=0;i<familyList.length;i++){
            family.addItem(familyList[i][0]);
        }
        familyPanel.add(family);
        info.add(familyPanel);

        add(info,BorderLayout.CENTER);


        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new UserList(userid);
            }
        });

        JButton add=new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(userName.getText().equals("")||phoneNum.getText().equals("")||gender.getText().equals("")
                        ||dob.getText().equals("")||family.getSelectedIndex()==0){
                    JOptionPane.showMessageDialog
                            (null, "Please Fill in all information!","Missing Input",
                                    JOptionPane.ERROR_MESSAGE);

                }
                else{

                    String newPK="";
                    boolean isSame=true;
                    while(isSame){

                        isSame=false;
                        newPK="u";


                        int randomNum=(int)(Math.random()*9)+49;
                        newPK=newPK+""+(char)randomNum;

                        for(int i=1;i<=7;i++){

                            newPK=newPK+(char)((int)(Math.random()*10)+48);
                        }
                        String[] pkID={"uUID"};
                        Object[][] pk=sql.search("User",pkID,"shanchu",1);
                        for(int i=0;i<pk.length;i++){
                            if(pk[i][0].equals(newPK)){
                                isSame=true;
                                break;
                            }
                        }
                    }


                    String randomPass="";
                    for(int i=1;i<=8;i++){
                        randomPass=randomPass+""+(char)((int)(Math.random()*26)+97);
                    }




                    Object[] newUserInfo=
                            {phoneNum.getText(),gender.getText(),dob.getText(),1,randomPass,newPK,family.getSelectedItem()};

                    sql.add(newUserInfo,"User");

                    Object[] newAccountInfo={newPK,userName.getText(),randomPass,"1"};

                    sql.add(newAccountInfo,"Account");


                    JOptionPane.showMessageDialog
                            (null, "Add Complete!Random uUID:"+newPK,
                                    "Success",JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);

                    new UserList(userid);


                }


            }
        });




        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(add);

        add(buttons,BorderLayout.SOUTH);






        setSize(500,420);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);




    }
}
